<?php

/**
 * paciente actions.
 *
 * @package    dentist
 * @subpackage paciente
 * @author     Your name here
 */
class pacienteActions extends sfActions {

    public function executeIndex(sfWebRequest $request) {
        $usuarioId = $this->getUser()->getAttribute('id');
        $this->Pacientes = PacientePeer::getPacientes($usuarioId);
    }

    public function executeShow(sfWebRequest $request) {
        $this->Paciente = PacientePeer::retrieveByPk($request->getParameter('id'));
        $this->forward404Unless($this->Paciente);
    }

    public function executeNew(sfWebRequest $request) {
        $this->form = new PacienteForm();
    }

    public function executeCreate(sfWebRequest $request) {
        $this->forward404Unless($request->isMethod(sfRequest::POST));

        $this->form = new PacienteForm();

        $this->processFormCreate($request, $this->form);

        $this->setTemplate('new');
    }

    public function executeEdit(sfWebRequest $request) {
        $this->forward404Unless($Paciente = PacientePeer::retrieveByPk($request->getParameter('id')), sprintf('Object Paciente does not exist (%s).', $request->getParameter('id')));
        $this->form = new PacienteForm($Paciente);
    }

    public function executeUpdate(sfWebRequest $request) {
        $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
        $this->forward404Unless($Paciente = PacientePeer::retrieveByPk($request->getParameter('id')), sprintf('Object Paciente does not exist (%s).', $request->getParameter('id')));
        $this->form = new PacienteForm($Paciente);

        $this->processFormUpdate($request, $this->form);

        $this->setTemplate('edit');
    }

    public function executeDelete(sfWebRequest $request) {
        $request->checkCSRFProtection();

        $this->forward404Unless($Paciente = PacientePeer::retrieveByPk($request->getParameter('id')), sprintf('Object Paciente does not exist (%s).', $request->getParameter('id')));
        $Paciente->delete();

        $this->redirect('paciente/index');
    }

    protected function processFormCreate(sfWebRequest $request, sfForm $form) {
        $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
        $usuarioId = $this->getUser()->getAttribute('id');

        if ($form->isValid()) {
            $valores = $this->form->getValues();

            // se busca el paciente en la base para ver si ya existe
            if (PacientePeer::getPaciente($valores['cedula'], $usuarioId) == null) {

                $Paciente = new Paciente();
                $Paciente->setNombre($valores['nombre']);
                $Paciente->setApellido($valores['apellido']);
                $Paciente->setCedula($valores['cedula']);
                $Paciente->setDireccion($valores['direccion']);
                $Paciente->setTelefono($valores['telefono']);
                $Paciente->setCorreo($valores['correo']);
                $Paciente->setFechaNac($valores['fecha_nac']);
                $Paciente->setFechaIng($valores['fecha_ing']);
                $Paciente->setFechaBaja($valores['fecha_baja']);
                $Paciente->setNroPaciente($valores['nro_paciente']);
                $Paciente->setActivo($valores['activo']);
                $Paciente->setUsuarioId($usuarioId);
                $Paciente->save();

                $this->redirect('paciente/show?id=' . $Paciente->getId());
            } else {
                $this->getUser()->setFlash("error", "Error al guardar los datos, por favor verifíquelos y vuelva a intentar.");
            }
        }
    }

    protected function processFormUpdate(sfWebRequest $request, sfForm $form) {
        $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
        if ($form->isValid()) {
            $Paciente = $form->save();

            $this->redirect('paciente/show?id=' . $Paciente->getId());
        } else {
            $this->getUser()->setFlash("error", "Error al guardar los datos, por favor verifíquelos y vuelva a intentar.");
        }
    }

    /**
     * Hello world test.
     */
    public function executeListadoPacientesPDF() {
        $config = sfTCPDFPluginConfigHandler::loadConfig();

        // pdf object
        $pdf = new sfTCPDF();

        // set document information
        $pdf->SetCreator(PDF_CREATOR);
        $pdf->SetAuthor('Sistema Dentist');
        $pdf->SetTitle('Listado de pacientes');
        $pdf->SetSubject('Listado completo de pacientes');
        $pdf->SetKeywords('Listado, Pacientes');

        // set default header data
        $pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, 'Listado de pacientes', PDF_HEADER_STRING);

        // set header and footer fonts
        $pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
        $pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));

        // set default monospaced font
        $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

        //set margins
        $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
        $pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
        $pdf->SetFooterMargin(PDF_MARGIN_FOOTER);

        //set auto page breaks
        $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

        //set image scale factor
        $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);

        // ---------------------------------------------------------
        // set default font subsetting mode
        $pdf->setFontSubsetting(true);

        // Set font
        // dejavusans is a UTF-8 Unicode font, if you only need to
        // print standard ASCII chars, you can use core fonts like
        // helvetica or times to reduce file size.
        $pdf->SetFont('dejavusans', '', 10, '', true);

        // Add a page
        $pdf->AddPage();

        $usuarioId = $this->getUser()->getAttribute('id');
        $Pacientes = PacientePeer::getPacientes($usuarioId);

        $html = '<h1>Listado de pacientes</h1>';
        $html .= '<table border="1"><thead><tr>';
        $html .= '<th>Nombre</th>';
        $html .= '<th>Apellido</th>';
        $html .= '<th>Cedula</th>';
        $html .= '<th>Direccion</th>';
        $html .= '<th>Telefono</th>';
        $html .= '</tr></thead><tbody>';
        
        foreach ($Pacientes as $Paciente) {
            $html .= '<tr>';
            $html .= '<td>' . $Paciente->getNombre() . '</td>';
            $html .= '<td>' . $Paciente->getApellido() . '</td>';
            $html .= '<td>' . $Paciente->getCedula() . '</td>';
            $html .= '<td>' . $Paciente->getDireccion() . '</td>';
            $html .= '<td>' . $Paciente->getTelefono() . '</td>';
            $html .= '</tr>';
        }
        $html .= '</tbody></table>';

        // output some HTML code
        $pdf->writeHTML($html, true, 0);

        // ---------------------------------------------------------
        // Close and output PDF document
        // This method has several options, check the source code documentation for more information.
        $pdf->Output('listadoDePacientes.pdf', 'I');

        // Stop symfony process
        throw new sfStopException();
    }

}

